/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.proxy.component
{
	/**
	 * 构造服务请求命令对象 
	 * 
	 */	
	public class SPOParam extends POParam  implements IPOParam
	{
		private var _serviceName:String;
		private var _dsName:String;
		private var _methods:String;
		private var _params: Array;
		private var _obj:Object;		
		private var _isFileRequest:Boolean=false;
		
		/**
		 * 构造服务请求命令串
		 * <ds>
		 *	  <caller/>
		 *	  <appName></appName>
		 *	  <SPOParam>
		 *	    <serviceName></serviceName> 例如：Services.serviceGetServerUnits
		 *	    <dsName></dsName>
		 *	    <methods/>
		 *	    <parameters>
		 *	      <param name="unitId"></param>
		 *	    </parameters>
		 *	  </SPOParam>
		 *	</ds>
		 * 
		 *  使用方法如下： 
		 * 
		 *  var spo:SPOParam=new SPOParam();
		 *	spo.serviceName="服务文件名.过程名";
		 *	spo.addParameters("id",txtUser.text);
		 *	spo.dsName="getUser";
		 */		
		public function SPOParam()
		{
			_params= new Array;
		}
		
		/**
		 * 服务名；对应toXML()中的    <serviceName></serviceName> 
		 * @param value
		 * 
		 */		
		public function set serviceName(value:String):void
		{_serviceName=value;}
		public function get serviceName():String
		{return _serviceName;} 	
		
		/**
		 * 方法名；对应toXML()中的<methods/> 
		 * @param value
		 * 
		 */		
		public function set methods(value:String):void
		{_methods=value;}
		public function get methods():String
		{return _methods;}
		
		/**
		 * 服务名；对应toXml()中的 <dsName></dsName> 
		 * @param value
		 * 
		 */		
		public function set dsName(value:String):void
		{_dsName=value;}
		public function get dsName():String
		{return _dsName;} 		
		
		/**
		 * 是否为文件请求.默认为false
		 */
		public function set isFileRequest(value:Boolean):void{
			_isFileRequest=value;
		}
		public function get isFileRequest():Boolean{
			return _isFileRequest;
		}
		
		
		/**
		 * 参数添加；如toxml()方法中的<param name="unitId"></param> 
		 * @param paraName 参数名
		 * @param paraValue 参数值
		 * 
		 */		
		public function addParameters(paraName:String,paraValue:String):void
		{
			_obj=new Object();
			_obj["paraName"]=paraName;
			_obj["paraValue"]=paraValue;
			_params.push(_obj);			
		}
		
		/**
		 * 返回参数集合。 
		 * @return 
		 * 
		 */		
		public function get parameters():Array
		{
			return _params;
		}	
		
		/**
		 * 组成SPoparam协议串；格式如下 
		 * @return 
		 *  <ds>
		 *	  <caller/>
		 *	  <appName></appName>
		 *	  <SPOParam>
		 *	    <serviceName></serviceName> 例如：Services.serviceGetServerUnits
		 *	    <dsName></dsName>
		 *	    <methods/>
		 * 		<isFileRequest>false</isFileRequest>
		 *	    <parameters>
		 *	      <param name="unitId"></param>
		 *	    </parameters>
		 *	  </SPOParam>
		 *	</ds>
		 */		
		override  public function toXml():XML
		{
			var pxml:XML=new XML(<SPOParam />);
			var i:int;
			pxml.appendChild(<serviceName>{serviceName}</serviceName>);
			pxml.appendChild(<dsName>{dsName}</dsName>);
			pxml.appendChild(<methods>{methods}</methods>);
			pxml.appendChild(<isFileRequest>{isFileRequest}</isFileRequest>);
			pxml.appendChild(<parameters />);
			for(i=0;i<_params.length;i++)
			{
				pxml.parameters.appendChild(<param name={_params[i]["paraName"]}>
					{cdata(_params[i]["paraValue"])}
				 </param>);
			}
			return pxml;
		}
		
		private function cdata(value:String):XML
		{
			return  new XML("<![CDATA[" + value + "]]>");
		}	
	}
}